const { contextBridge, ipcRenderer } = require("electron");
const { SerialPort } = require("serialport");

contextBridge.exposeInMainWorld("ipcRenderer", {
  send(...args) {
    ipcRenderer.send(...args);
  },
});

contextBridge.exposeInMainWorld("serialPort", {
  list() {
    return SerialPort.list();
  },
  create(options, callback) {
    const port = new SerialPort(options, callback);
    return {
      write: (data) => port.write(data),
      open: () => port.open(),
      close: () => port.close(),
      on: (event, callback) => port.on(event, callback),
      onError: (callback) => port.on("error", callback),
      isOpen: () => port.isOpen,
      // 你可以在这里暴露更多的方法或属性
    };
  },
});
